package com.example.take_out_dish.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.take_out_admin_api.AdminClient;
import com.example.take_out_dish.common.lang.dto.CategoryDto;
import com.example.take_out_dish.entity.Category;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.CategoryMapper;
import com.example.take_out_dish.mapper.DishMapper;
import com.example.take_out_dish.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private AdminClient adminClient;

    @Resource
    private HttpServletRequest request;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void saveCategory(CategoryDto categoryDto) {
        // TODO: 封装该方法 用拦截器
        String token = request.getHeader("Authorization");
        log.info("token:{}", token);
        if(token==null) {
            throw new BusinessException(ResponseEnum.TOKEN_ERROR);
        }
        if(!adminClient.validateToken(token)) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }

        String name = adminClient.parseJwt(token);
        log.info("name:{}", name);

        /**
         {
         "name": "admin",
         "role": "",
         "adminId": "1",
         "iat": 1720191463,
         "exp": 1720195063
         }
         */

        Category category = new Category();
        BeanUtil.copyProperties(categoryDto, category, "createTime", "updateTime");
        category.setCreateUser(name);
        category.setUpdateUser(name);

        this.save(category);
    }

    @Override
    public void deleteCategoryById(String id) {
        String token = request.getHeader("Authorization");
        log.info("token:{}", token);
        if(token==null) {
            throw new BusinessException(ResponseEnum.TOKEN_ERROR);
        }
        if(!adminClient.validateToken(token)) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }

        //检查这个分类下面有没有菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        long count = dishMapper.selectCount(wrapper);
        if(count>0){
            throw new BusinessException(ResponseEnum.CATEGORY_ERROR);
        }

        this.removeById(id);
    }

    @Override
    public void updateCategoryById(CategoryDto categoryDto) {

        String token = request.getHeader("Authorization");
        log.info("token:{}", token);
        if(token==null) {
            throw new BusinessException(ResponseEnum.TOKEN_ERROR);
        }
        if(!adminClient.validateToken(token)) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }

        String name = adminClient.parseJwt(token);
        log.info("name:{}", name);

        Category category = new Category();
        BeanUtil.copyProperties(categoryDto, category, "updateTime", "createTime");

        category.setCreateUser(name);
        category.setUpdateUser(name);
        this.updateById(category);
    }

    @Override
    public void startOrStop(Integer status, String id) {
        Category category = this.getById(id);
        if (category==null) {
            throw new BusinessException(ResponseEnum.NOT_EXIST);
        }
        category.setStatus(status>0 ? 1:0);
        this.updateById(category);
    }

    @Override
    public List<Category> list(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>();
        wrapper.eq(Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime);
        List<Category> list = this.list(wrapper);
        return list;
    }
}
